package com.example.utils.impl;

import com.example.utils.DealNumber;
import com.example.utils.ImgUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class ImgUtilImpl implements ImgUtil {

    private final static String STORGE_PATH = "C:\\Users\\Ten.X\\Desktop\\AlbumImg\\";

//    private final static String STORGE_PATH = "/usr/AlbumImg/";

    @Override
    public String upload(MultipartFile file) {
        try {
            if (file.isEmpty()) {
                return "文件为空";
            }
            //获取文件名
            String fileName = file.getOriginalFilename();
            log.info("上传的文件名：" + fileName);
            //获取文件后缀名
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            log.info("文件后缀名：" + suffixName);
            String myname = DealNumber.imgcode() + suffixName;
            //设置文件存储路径
            String filePath = STORGE_PATH;
            String path = filePath + myname;
            File dest = new File(path);
            //检测是否存在该目录
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            //写入文件
            file.transferTo(dest);
            return myname;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "上传失败";
    }

    @Override
    public String downloadFile(String fileName, HttpServletResponse response) {
        if (fileName != null) {
            //设置文件路径
            File file = new File(STORGE_PATH + fileName);
            if (file.exists()) {
                response.setContentType("application/force-download");// 设置强制下载不打开
                response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    return "下载成功";
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return "下载失败";
    }

    public List<String> uploadFilesmore(List<MultipartFile> files) {
        List<String> finalmsg = new ArrayList<>();
        System.out.println(files);
        for (MultipartFile file : files) {
            try {
                if (file.isEmpty()) {
                    finalmsg.add("文件为空");
                    return finalmsg;
                }
                //获取文件名
                String fileName = file.getOriginalFilename();
                log.info("上传的文件名：" + fileName);
                //获取文件后缀名
                String suffixName = fileName.substring(fileName.lastIndexOf("."));
                log.info("文件后缀名：" + suffixName);
                String myname = DealNumber.imgcode() + suffixName;
                //设置文件存储路径
                String filePath = STORGE_PATH;
                String path = filePath + myname;
                File dest = new File(path);
                //检测是否存在该目录
                if (!dest.getParentFile().exists()) {
                    dest.getParentFile().mkdirs();
                }
                //写入文件
                file.transferTo(dest);
                finalmsg.add(myname);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return finalmsg;
    }
}
